#include "MediaFileChild.h"
#include <string.h>
#include "faacapi.h"
MediaFile*OpenMediaFileWithAAC(const char*filename,int FileType){
    MediaFile*mf=NULL;
    FILE_FAAC faacinfo={0};
    int i=0;
    if(FAACGetAudioSpecificConfig(filename,&faacinfo)==-1)
       return NULL;
    mf=MallocMediaFile();
    mf->FileType=FILE_TYPE_AAC;
    mf->AudioType=AUDIO_TYPE_AAC;
    mf->fp=faacinfo.fp;
    mf->AudioInfo.nChannels=faacinfo.frameinfo.nb_ch;
    mf->AudioInfo.nSamplesPerSec=FAACGetSampleRatesByIndex(faacinfo.frameinfo.sr_idx);
    mf->AudioInfo.wBitsPerSample=16;
    mf->AACInfo.aaclen=faacinfo.frameinfo.frame_size;
    mf->AACInfo.aac=malloc(mf->AACInfo.aaclen);
    FAACGetNextFrameSize(&faacinfo);
    FAACGetFrameBuff(&faacinfo,mf->AACInfo.aac,mf->AACInfo.aaclen);
    while(1){
      uint32_t NextFrameSize=FAACGetNextFrameSize(&faacinfo);
      MediaFrame*frame=NULL;
      if(NextFrameSize==0)break;
      frame=MallocMediaFrame(0);
      frame->pos=ftell(mf->fp);
      frame->datalen=NextFrameSize;
      frame->CodecType=AUDIO_TYPE_AAC;
      frame->KeyFrame=1;
      frame->FrameType=FRAME_TYPE_AUDIO;
      frame->timestamp=(uint32_t)i*1024*1000.0/mf->AudioInfo.nSamplesPerSec;
      mf->TotalTime=frame->timestamp;
      AddMediaFrame(mf,frame);
      i++;
      //printf("%d %u %u\n",i,NextFrameSize,frame->timestamp);
      fseek(mf->fp,NextFrameSize,SEEK_CUR);
    }
    mf->CurrentFrame=mf->FrameList;
    return mf;
}